import sqlite3  
from sqlite3 import Error  
  
class SQLiteDB:  
    def __init__(self, db_file):  
        """  
        初始化SQLite数据库连接  
        :param db_file: 数据库文件路径  
        """  
        self.conn = None  
        try:  
            self.conn = sqlite3.connect(db_file)  
            print("SQLite connection is established")  
        except Error as e:  
            print(f"The error '{e}' occurred")  
  
    def __del__(self):  
        """  
        析构方法，用于关闭数据库连接  
        """  
        if self.conn:  
            self.conn.close()  
            print("SQLite connection is closed")  
  
    def create_table(self, create_table_sql):  
        """  
        创建表  
        :param create_table_sql: 创建表的SQL语句  
        """  
        try:  
            c = self.conn.cursor()  
            c.execute(create_table_sql)  
            self.conn.commit()  
            print("Table created successfully")  
        except Error as e:  
            print(f"The error '{e}' occurred")  
  
    def read_data(self, select_query):  
        """  
        读取数据  
        :param select_query: 查询的SQL语句  
        :return: 查询结果  
        """  
        cur = self.conn.cursor()  
        cur.execute(select_query)  
        rows = cur.fetchall()  
        return rows  
  
    def insert_data(self, insert_query, data):  
        """  
        插入数据  
        :param insert_query: 插入数据的SQL语句  
        :param data: 要插入的数据（元组或列表）  
        """  
        try:  
            c = self.conn.cursor()  
            c.execute(insert_query, data)  
            self.conn.commit()  
            print("Record inserted successfully")  
        except Error as e:  
            print(f"The error '{e}' occurred")  
  
    def update_data(self, update_query, data):  
        """  
        更新数据  
        :param update_query: 更新数据的SQL语句  
        :param data: 要更新的数据（元组或列表）  
        """  
        try:  
            c = self.conn.cursor()  
            c.execute(update_query, data)  
            self.conn.commit()  
            print("Record updated successfully")  
        except Error as e:  
            print(f"The error '{e}' occurred")  
  
    def delete_data(self, delete_query, data):  
        """  
        删除数据  
        :param delete_query: 删除数据的SQL语句  
        :param data: 删除条件的数据（元组或列表）  
        """  
        try:  
            c = self.conn.cursor()  
            c.execute(delete_query, data)  
            self.conn.commit()  
            print("Record deleted successfully")  
        except Error as e:  
            print(f"The error '{e}' occurred")  
  
# 示例用法  
if __name__ == '__main__':  
    database = SQLiteDB('example.db')  
  
    # 创建表  
    database.create_table('''CREATE TABLE IF NOT EXISTS COMPANY  
             (ID INT PRIMARY KEY     NOT NULL,  
             NAME           TEXT    NOT NULL,  
             AGE            INT     NOT NULL,  
             ADDRESS        CHAR(50),  
             SALARY         REAL);''')  
  
    # 插入数据  
    database.insert_data("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) VALUES (?,?,?,?,?)",  
                         (1, 'Paul', 32, 'California', 20000.00))  
  
    # 读取数据  
    print(database.read_data("SELECT * FROM COMPANY"))  
  
    # 更新数据  
    database.update_data("UPDATE COMPANY SET SALARY = ? WHERE ID = ?", (25000.00, 1))  
  
    # 删除数据  
    database.delete_data("DELETE FROM COMPANY WHERE ID = ?", (1,))  
  
    # 再次读取数据以验证更新和删除  
    print(database.read_data("SELECT * FROM COMPANY"))